﻿using Domain.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

namespace Domain.Context
{
    public partial class RepositoryContext
    {

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            ConfigureModel(modelBuilder);
            ConfigureRelationship(modelBuilder);
            ConfigureQueryFilter(modelBuilder);
            InitSeedData(modelBuilder);
        }
        /// <summary>
        /// 配置数据模型
        /// </summary>
        /// <param name="modelBuilder"></param>
        private void ConfigureModel(ModelBuilder modelBuilder)
        {
            #region Account
            modelBuilder.Entity<Account>(p =>
            {
                p.HasIndex(x => x.UserName)
                 .IsUnique();
            });
            #endregion

            #region AuthToken
            modelBuilder.Entity<AuthToken>(p =>
            {
                p.Property(x => x.CreateTime).HasDefaultValueSql("GETDATE()");
                p.Property(x => x.UpdateTime).HasDefaultValueSql("GETDATE()");
            });
            #endregion

            modelBuilder.Entity<RolePermission>().HasKey(rp => new { rp.RoleId, rp.PermissionId });
            modelBuilder.Entity<AccountRole>().HasKey(ar => new { ar.AccountId, ar.RoleId });
            modelBuilder.Entity<PermissionNavigation>().HasKey(nr => new { nr.NavigationId, nr.PermissionId });
            modelBuilder.Entity<CompanyDepartment>().HasKey(cd => new { cd.CompanyId, cd.DepartmentId });
            modelBuilder.Entity<CompanyDepartment>().HasKey(c => new { c.DepartmentId, c.CompanyId });
            modelBuilder.Entity<Employee>().Property(e => e.Gender)
               .HasConversion(new EnumToStringConverter<Gender>());
            modelBuilder.Entity<Company>().OwnsOne(p => p.Address);
        }
        /// <summary>
        /// 配置数据关系
        /// </summary>
        /// <param name="modelBuilder"></param>
        private void ConfigureRelationship(ModelBuilder modelBuilder)
        {
            #region RDAC权限配置




            //modelBuilder.Entity<AccountGroup>().HasKey(ag => new { ag.GroupId, ag.AccountId });
            //modelBuilder.Entity<GroupRole>().HasKey(gr => new { gr.GroupId, gr.RoleId });




            //modelBuilder.Entity<Group>()
            //    .HasOne(g => g.Parent)
            //    .WithMany(g => g.Children)
            //    .IsRequired(false)
            //    .HasForeignKey(g => g.ParentId)
            //    .OnDelete(DeleteBehavior.Restrict);

            #endregion






            modelBuilder.Entity<Navigation>()
                .HasOne(n => n.Parent)
                .WithMany(n => n.Children)
                .IsRequired(false)
                .HasForeignKey(x => x.ParentId)
                .OnDelete(DeleteBehavior.NoAction);
        }


        /// <summary>
        /// 配置查询过滤
        /// </summary>
        /// <param name="modelBuilder"></param>
        private void ConfigureQueryFilter(ModelBuilder modelBuilder)
        {
            //modelBuilder.Entity<Account>().HasQueryFilter(a => a.EntityStatus == EntityStatus.Active);
        }
        #region 配置数据的更新和添加的属性值
        /// <summary>
        /// 配置数据UpdateTime和CreateTime的一种方式
        /// </summary>
        /// <returns></returns>
        //public override int SaveChanges()
        //{
        //    var entries = ChangeTracker.Entries()
        //                 .Where(x => x.Entity is BaseEntity
        //                             && (
        //                                x.State == EntityState.Modified
        //                                || x.State == EntityState.Added));
        //    foreach (var item in entries)
        //    {
        //        ((BaseEntity)item.Entity).UpdateTime = DateTime.Now;
        //        if (item.State == EntityState.Added)
        //        {
        //            ((BaseEntity)item.Entity).CreateTime = DateTime.Now;
        //        }
        //    }
        //    return base.SaveChanges();
        //}
        #endregion

    }
}
